package com.rdk.wechat.request.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rdk.exception.ServiceException;
import com.rdk.utils.HttpClientUtil;
import com.rdk.wechat.common.Constant;
import com.rdk.wechat.entity.WechatServiceUrlEntity;
import com.rdk.wechat.request.UserWechatRquest;

/**
 * 用户管理请求服务接口
 * 包含4个基础服务请求：
 * 1.获取UserToken
 * 2.获取用户基本信息
 * 3.获取好友列表分页时间戳
 * 4.获取用户好友关注列表
 * 
 * @author wanglei
 *
 */
public class UserWechatRquestImpl  extends BaseRequest implements UserWechatRquest {
	
	private Logger log = LoggerFactory.getLogger(UserWechatRquestImpl.class);
	
	public UserWechatRquestImpl(String accessToken) {
		super(accessToken);
		we = new WechatServiceUrlEntity();
	}
	
	/**
	 * 获取用户登录的唯一票据
	 * userToken只能使用一次，并且有效期为3分钟
	 * 
	 * @param accessToken 接入票据
	 * @param clientKey 接入的key值，从豆豆中或者是登录系统获取的登录票据
	 * 
	 * @return json String
	 */
	@Override
	public String getUserToken(String clientKey) {
		we.setServiceName(Constant.wechatServiceName.USERTOKEN);
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("clientKey", clientKey);
		we.setParamMap(paramMap);
		String url = this.getUrl(we);
		
		System.out.println(url);
		
		String jsonStr = null;
		try {
			jsonStr = httpClientUtil.getData(url);

		} catch (ServiceException e) {
			e.printStackTrace();
			
		}
		//String url = 
		return jsonStr;
	}
	
	/**
	 * 获取用户基本信息
	 * 通过该接口获取用户基本信息，包括用户ID、昵称、头像、
	 * 性别、所在城市、语言等；非企业用户，不能获取。 
	 * 
	 * @param userToken 用户接入凭证
	 * @param lang 返回国家地区语言版本zh_CN 简体、zh_TW 繁体、en 英语 
	 *  
	 * @return json String
	 */
	@Override
	public String getUserInfo(String userToken, String lang) {
		// TODO Auto-generated method stub
		
		we.setServiceName(Constant.wechatServiceName.USER);
		we.setOpName(Constant.wechatOpName.INFO);
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("user_token", userToken);
		paramMap.put("lang", lang);
		we.setParamMap(paramMap);
		String url = this.getUrl(we);
		System.out.println(url);
		String jsonStr = null;
		try {
			jsonStr = httpClientUtil.getData(url);

		} catch (ServiceException e) {
			e.printStackTrace();
			
		}
		//String url = 
		return jsonStr;
		
	}
	
	
	/**
	 * 获取好友列表分页时间戳
	 * 通过该接口获取用户的好友分页时间戳判断好友分页信息，
	 * 根绝页码信息获取好友列表
	 * 
	 * 
	 * @return json String
	 */
	@Override
	public String getUserBuddysTimestamp(String userToken, String lang) {
		we.setServiceName(Constant.wechatServiceName.USER);
		we.setOpName(Constant.wechatOpName.BUDDYSTIMESTAMP);
		
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("user_token", userToken);
		paramMap.put("lang", lang);
		we.setParamMap(paramMap);
		String url = this.getUrl(we);
		System.out.println(url);
		String jsonStr = null;
		try {
			jsonStr = httpClientUtil.getData(url);

		} catch (ServiceException e) {
			e.printStackTrace();
			
		}
		//String url = 
		return jsonStr;
	}

	/**
	 * 获取用户好友关注列表
	 * 
	 * @param userToken 用户接入凭证
	 * @param lang 返回国家地区语言版本zh_CN 简体、zh_TW 繁体、en 英语 
	 * @param pageNo 页码
	 * 
	 * @return json String
	 */
	@Override
	public String getUserBuddysToPaging(String userToken, String lang, String pageNo) {
		we.setServiceName(Constant.wechatServiceName.USER);
		we.setOpName(Constant.wechatOpName.BUDDYS);
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("user_token", userToken);
		paramMap.put("lang", lang);
		paramMap.put("page_no", pageNo);
		we.setParamMap(paramMap);
		String url = this.getUrl(we);
		System.out.println(url);
		String jsonStr = null;
		try {
			jsonStr = httpClientUtil.getData(url);
		} catch (ServiceException e) {
			e.printStackTrace();
			
		}
		//String url = 
		//String jsonStr = "{\"code\":0,\"msg\":\"\",\"result\":[{\"userID\":4404019199,\"name\":\"IM团队\",\"sex\":2,\"status\":1,\"portraitURL\":\"hd1/head/20140113/1500/head_Q5Va_494300000001003b.jpg\",\"remark\":\"IM团队\"}]}";
		return jsonStr;
	}
	
	
	
	public static void main(String[] args) {
		/*String token = "lTZsO7tzMFrcbaCWFrHOhurKv20nJu6_ZVv5OsXFttGsba1ySmKeYk9QYa3ZB4jU3xSMSjR00YVc3bzqCMPCPvJ7hYRYeq07sJxWXjMJWR0";
		UserWechatRquest user = new UserWechatRquestImpl(token);
		String clientKey = "Kkj8kHRLCzZbB3T6p44YW7OagKxV2lQvPxhUGo6F8iZGOB6JEmudkY1MH8k0b21U";
		
		//String json = user.getUserBuddysToPaging("4395732358", "en", "1");
		String json = user.getUserToken(clientKey);
		System.out.println(json);
		Map<String, Object> map = JSONutils.json2Map(json);
		if ((int)map.get("code") == 0){
			
			Map<String, Object> mTmep = (Map<String, Object>)map.get("result");
			
			System.out.println(map);
		}else {
			String msg = (String)map.get("msg");
		}*/
		//System.out.println(user.getUserInfo("4395732358", "en"));
		//System.out.println(user.getUserBuddysToPaging("4395732358", "en", "1"));
		
		try {
		
			HttpClientUtil httpClientUtil = new HttpClientUtil();
			httpClientUtil.setHttpclient(new DefaultHttpClient());
			
			String url = "http://192.168.0.59:5000/msg";
			
			 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			 NameValuePair pair=new BasicNameValuePair("body", "GETVERSION");
			 nameValuePairs.add(pair);
			
			String jsonStr = httpClientUtil.postData(url,nameValuePairs);
			
			System.out.println(jsonStr);
		} catch (ServiceException e) {
			e.printStackTrace();
			
		}
		
	}

}
